package com.dqp.cslggroup.Api;

import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.ConnectSever.SeverIP;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.AppUtils;
import com.dqp.cslggroup.Util.Result;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.Util.StringUtil;
import com.dqp.cslggroup.bean.Club;
import com.dqp.cslggroup.bean.Version;
import com.dqp.cslggroup.bean.push;
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @author 董琴平
 * @version 1.0
 * @date 2021/8/31 23:55
 */
public class appApi {
    static String ip = SharedPreferencesUtil.getIp(MyApplication.getContext());

    /*
     * @Description: 获取push通知
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/26
     */
    public static push getPush() {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(ip + "/cslg/app/getPush")
                    .method("GET", null)
                    .build();
            Response response = client.newCall(request).execute();
            if (response.body() == null) return null;
            String json = response.body().string();
            if (!StringUtil.isJson(json)) return null;
            JSONObject jsonObject = JSON.parseObject(json);
            if (!StringUtil.checkResult(jsonObject)) return null;
            if (jsonObject.containsKey("push")) {
                JSONObject object = jsonObject.getJSONObject("push");
                return object.toJavaObject(push.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }


    /*
     * @Description: push已读
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/26
     */
    public static void readPush(String id) {
        new Thread(() -> {
            try {
                if (ip == null) ip = SeverIP.getDefvalueUrl();
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .build();
                RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                        .addFormDataPart("id", id)
                        .build();
                Request request = new Request.Builder()
                        .url(ip + "/cslg/app/readPush?id=" + id)
                        .method("GET", null)
                        .build();
                client.newCall(request).execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    /*
     * @Description: 更新app
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/26
     */
    public static Version update(String system) {
        if (ip == null) ip = SeverIP.getDefvalueUrl();
        int getthisVersion = AppUtils.getVersionCode(MyApplication.getContext());
        String oldVersionName = AppUtils.getVersionName(MyApplication.getContext());
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(ip + "/cslg/app/update?system=" + system)
                    .method("GET", null)
                    .build();
            Response response = client.newCall(request).execute();
            if (response.body() == null) return null;
            String json = response.body().string();
            if (!StringUtil.isJson(json)) return null;
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject == null) return null;
            if (Result.jsonResultIsTure(jsonObject)) {
                JSONObject object = jsonObject.getJSONObject("version");
                if (object != null) {
                    int versionCode = object.getIntValue("versionCode");
                    String versionName = object.getString("versionName");
                    String des = object.getString("describes");
                    String dates = object.getString("dates");
                    boolean update = false;
                    if (versionCode > getthisVersion) update = true;
                    Version ver = new Version();
                    ver.setNewVersion("最新版本：" + versionName);
                    ver.setVersionName("最新版本：" + versionName);
                    ver.setOldVersionName("当前版本：" + oldVersionName);
                    ver.setUpdate(update);
                    ver.setInfo(des + "");
                    ver.setVersionCode(versionCode + "");
                    ver.setOldVersion(getthisVersion + "");
                    ver.setDate("发布时间：" + dates);
                    return ver;
                }
            }
        } catch (IOException | Error e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }


    /*
     * @Description: 获取banner
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/26
     */
    public static ArrayList<Club> getLoginImg() {
        if (ip == null) ip = SeverIP.getDefvalueUrl();
        ArrayList<Club> dataList = new ArrayList<>();
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(ip + "/cslg/app/getBannerImg")
                    .method("GET", null)
                    .build();
            Response response = client.newCall(request).execute();
            if (response.body() == null) return null;
            String json = response.body().string();
            if (!StringUtil.isJson(json)) return null;
            JSONObject jsonObject = JSON.parseObject(json);
            JSONArray clubs = JSON.parseArray(jsonObject.getString("banner"));
            if (!clubs.isEmpty()) {
                for (Object o : clubs) {
                    JSONObject x = (JSONObject) o;
                    Club club = new Club();
                    club.setClub_name(x.getString("title"));
                    club.setClub_img(x.getString("path"));
                    club.setClub_des(x.getString("type"));
                    dataList.add(club);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }
}
